package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.ShoppingCartDao;
import com.aaa.vegetablemarketexpress.entity.ShoppingCart;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.ShoppingCartService;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @FileName: ShoppingCartServiceImpl
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/18 10:23
 * @Version: 1.0.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartDao shoppingCartDao;
    @Resource
    private HttpSession session;

    /**
     * 添加购物车商品记录
     * @param shoppingCart
     * @return
     */
    @Override
    public Result addToCart(ShoppingCart shoppingCart) {
        // 从 session 中获取登录用户信息
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultUtil.failed("用户未登录");
        }
        Long userId = userInfo.getUserId();
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("productId", shoppingCart.getProductId());
        // 检查购物车中是否已存在该商品
        Integer exists = shoppingCartDao.checkCartExists(param);
        if (exists > 0) {
            // 已存在,更新购物车中商品的数量
            param.put("productCount", shoppingCart.getProductCount());
            shoppingCartDao.updateCartCount(param);
            return ResultUtil.success("购物车商品数量已更新");
        } else {
            //不存在 插入新的商品到购物车
            shoppingCart.setUserId(userId);
            shoppingCartDao.insertCart(shoppingCart);
            return ResultUtil.success("商品已添加到购物车");
        }

    }

    /**
     * 查询用户购物车列表
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> getCartList(Long userId) {
        return shoppingCartDao.getCartList(userId);
    }

    /**
     * 更新购物车商品数量
     * @param cartId 购物车ID
     * @param productCount 新数量
     * @return
     */
    @Override
    public Result updateCartItemCount(Long cartId, Integer productCount) {
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultUtil.failed("用户未登录");
        }

        Map<String, Object> param = new HashMap<>();
        param.put("cartId", cartId);
        param.put("productCount", productCount);

        shoppingCartDao.updateCartItemCount(param);
        return ResultUtil.success("数量已更新");
    }

    /**
     * 删除购物车商品
     * @param cartId 购物车ID
     * @return
     */
    @Override
    public Result deleteCartItem(Long cartId) {
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultUtil.failed("用户未登录");
        }

        shoppingCartDao.deleteCartItem(cartId);
        return ResultUtil.success("商品已删除");
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public Result clearCart() {
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultUtil.failed("用户未登录");
        }

        shoppingCartDao.clearCart(userInfo.getUserId());
        return ResultUtil.success("购物车已清空");
    }

    /**
     * 查询购物车商品总数量
     * @return
     */
    @Override
    public Result getCartItemCount() {
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultUtil.failed("用户未登录");
        }
        Long userId = userInfo.getUserId();
        Integer count = shoppingCartDao.getCartItemCount(userId);
        return ResultUtil.success(count);
    }
}
